<?php
$_SESSION['user_id']='1122';
class Course_selection_model extends CI_Model{



    function cSearch($keyword, $cate)
    {
        $this->load->database();
        $keyword = $this->input->post('keyword');
        $cate = $this->input->post('cate');
        $query = $this->db->query("SELECT * FROM career car, course_has_careers cc, course c,discipline d, requirements r WHERE (cc.course_id = c.course_id AND cc.career_id = car.career_id AND c.discipline_id = d.discipline_id AND r.discipline_id = d.discipline_id) AND ($cate LIKE '%$keyword%')");
        return $query->result();
    }

    function Addcourse($user_id){
        $id = $this->input->post('course_id');
        if (count($id) == 1) {
            $query = $this->db->query("INSERT INTO course_has_student(course_id,student_id) VALUES ('".$id[0]."','" .$user_id. "')");
         }else {
            foreach ($id as $ids) {
                 $query = $this->db->query("INSERT INTO course_has_student(course_id,student_id) VALUES ('".$ids."','" .$user_id. "')");
              }
        }
        return $query;
    }

    function mycourse($user_id){
        $query = $this->db->query("Select * FROM course_has_student cs, course c, discipline d, requirements r WHERE cs.student_id = " .$user_id." AND (cs.course_id = c.course_id AND c.discipline_id = d.discipline_id AND r.discipline_id = d.discipline_id)");
        return $query->result();
    }

    function mycourse_ss($user_id){
        $query = $this->db->query("Select * FROM career car, course_has_careers cc, course_has_student cs, course c, discipline d, requirements r WHERE cs.student_id = " . $user_id." AND (cc.course_id = c.course_id AND cc.career_id = car.career_id AND cs.course_id = c.course_id AND c.discipline_id = d.discipline_id AND r.discipline_id = d.discipline_id)");
        return $query->result();
    }




   function entry_delete() {
        $id = $this->input->post('course_id');
        if (count($id) == 1) {
            $query = $this->db->query("DELETE FROM course_has_student WHERE student_id = " . $user_id." AND course_id=".$id[0]);
         }else {
            foreach ($id as $ids) {
                $query = $this->db->query("DELETE FROM course_has_student WHERE student_id = " .$user_id." AND course_id=".$ids);
             }
        }
        return $query;
    }

    

    function scareer($user_id){
        $query = $this->db->query("Select * From discipline d, student_has_career sc, course_has_careers cc, course c, career ca where (d.discipline_id = c.discipline_id AND sc.career_id = cc.career_id AND cc.course_id = c.course_id AND sc.career_id = ca.career_id) AND sc.student_id = " . $user_id." order by ca.career_name");
        return $query->result();
    }

    

    function selCourseModel() {
        $ids = $this->input->post('course_id');
        $sql1 = array();
        foreach ($ids as $id) {
            $query = $this->db->query("SELECT * FROM course WHERE course_id =$id");
            $query = $query->result();
            foreach ($query as $sql) {
                $query1 = array($sql->course_name, $sql->course_id);
                array_unshift($sql1, $query1);
            }
        }
        return $sql1;
//        }
    }

    function confirmSelectionModel($id) {
        $sql = $this->db->query("SELECT * FROM course_has_student WHERE course_id =$id AND student_id =" . $user_id."");
        if (count($sql->result()) == 0) {
            $query = $this->db->query("INSERT INTO course_has_student(course_id,student_id) VALUES($id,'" . $user_id. "')");
            return $query;
        }
    }

    function showDesc($course_id) {
        $query = $this->db->query("SELECT course_description FROM course WHERE course_id=$course_id");
        return $query->result();
    }

    //Improved
    
    function SelectCareerToCourse($user_id){
        $this->load->database();
        $query = $this->db->query("Select * FROM student_has_career shc, career car, course_has_careers cc, course c, discipline d, requirements r WHERE shc.student_id = ".$user_id." AND (shc.career_id = cc.career_id AND cc.course_id = c.course_id AND cc.career_id = car.career_id AND c.discipline_id = d.discipline_id AND r.discipline_id = d.discipline_id)");
        return $query->result();
    }

     function searchcc($user_id,$keyword, $cate){
        $this->load->database();
        $keyword = $this->input->post('keyword');
        $cate = $this->input->post('cate');
        $query = $this->db->query("SELECT * FROM student_has_career shc, career car, course_has_careers cc, course c,discipline d, requirements r WHERE shc.student_id = ".$user_id." AND (shc.career_id = cc.career_id AND cc.course_id = c.course_id AND cc.career_id = car.career_id AND c.discipline_id = d.discipline_id AND r.discipline_id = d.discipline_id) AND ($cate LIKE '%$keyword%')");
        return $query->result();
    }


}
?>